16E - Fish - CodeForces Solution


bitmasks dp probabilities *1900

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n = int(input())
a = [list(map(float, input().split())) for _ in range(n)]
pow2 = [1]
for _ in range(n):
    pow2.append(2 * pow2[-1])
m = pow2[n]
dp = [0] * m
dp[-1] = 1
for i in range(m - 1, 0, -1):
    x = []
    for j in range(n):
        if i & pow2[j]:
            x.append(j)
    u = len(x) * (len(x) - 1) // 2
    if not u or not dp[i]:
        continue
    dpi = dp[i]
    for j in x:
        aj = a[j]
        for k in x:
            if j == k:
                continue
            l = i ^ pow2[k]
            dp[l] += dpi * aj[k] / u
ans = [dp[i] for i in pow2[:-1]]
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long int li;
typedef unsigned long int uli;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;

int main()
{
    int n;
    scd(n);
    vector<vector<ld>> vec(n, vector<ld>(n));

    frange(i, n)
    {
        frange(j, n)
        {
            cin >> vec[i][j];
        }
    }

    vector<ld> dp(1 << n, 0);
    frange(i, n)
    {
        dp[(1 << n) - 1] = 1;
    }
    for (int i = (1 << n) - 2; i >= 1; i--)
    {

        int k = __builtin_popcount(i) + 1;
        ld c = ld(k * ld(k - 1)) / 2;
        frange(j, n)
        {
            if (i & (1 << j))
            {
                frange(l, n)
                {
                    if ((i & (1 << l)) == 0)
                    {
                        dp[i] += dp[i ^ (1 << l)] * vec[j][l] / c;
                    }
                }
            }
        }
    }
    // frange(i, (1 << n))
    // {
    //     frange(j, n)
    //     {
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << "\n";
    // }
    cout << setprecision(6);
    frange(i, n)
    {
        cout << dp[1 << i] << " ";
    }
}


Comments

Submit
0 Comments
More Questions

740A - Alyona and copybooks
1491A - K-th Largest Value
922B - Magic Forest
922D - Robot Vacuum Cleaner
408B - Garland
1391A - Suborrays
1700C - Helping the Nature
982A - Row
877A - Alex and broken contest
919D - Substring
362B - Petya and Staircases
1535C - Unstable String
1738F - Connectivity Addicts
1342B - Binary Period
1551C - Interesting Story
794B - Cutting Carrot
534B - Covered Path
1278C - Berry Jam
1203A - Circle of Students
1740B - Jumbo Extra Cheese 2
1740A - Factorise N+M
49B - Sum
23A - You're Given a String
1105C - Ayoub and Lost Array
1624E - Masha-forgetful
998B - Cutting
250A - Paper Work
1740C - Bricks and Bags
1130A - Be Positive
465A - inc ARG